export default {
	data() {
		return {
			isIphoneX: false,
			orderCreateData: {},
			orderPaymentData: {
				exchange_info: {
					type: 0
				}
			},
			type: 1,
			isSub: false,
			award_id: '',
			record_id: '',
			address:{},
			goodsData:{},
			buyer_message:'',
			orderData:{}
		};
	},
	onLoad(option) {
		if (option.record_id) {
			this.record_id = option.record_id;
		}
		if (option.award_id) {
			this.award_id = option.award_id;
		}
		this.token = uni.getStorageSync('token');
	},
	computed: {
		// 消费券抵扣
		balanceDeduct() {
			if (parseFloat(this.goodsData.balance_total) <= parseFloat(this.goodsData.purchase_price)) {
				return parseFloat(this.goodsData.balance_total).toFixed(2);
			} else {
				return parseFloat(this.goodsData.purchase_price).toFixed(2);
			}
		}
	},
	methods: {
		// 显示选择支付方式弹框
		openChoosePayment() {
			if (this.verify() && this.goodsData.purchase_price > 0) this.$refs.choosePaymentPopup.open();
			else this.orderCreate();
		},
		// 使用消费券
		useBalance() {
			if (this.goodsData.is_balance) {
				this.goodsData.is_balance = 0;
				this.goodsData.pay_price = this.goodsData.purchase_price;
			} else {
				this.goodsData.is_balance = 1
				this.goodsData.pay_price = parseFloat(this.goodsData.purchase_price).toFixed(2) - parseFloat(this.balanceDeduct).toFixed(2);
			}
			this.$forceUpdate();
		},
		// 显示弹出层
		openPopup(ref) {
			this.$refs[ref].open();
		},
		// 关闭弹出层
		closePopup(ref) {
			this.$refs[ref].close();
		},
		// 选择收货地址，默认有定位
		selectAddress() {
			var params = {
				back: '/promotionpages/game/payment/payment',
				local: 0,
				type: 1
			}
			this.$util.redirectTo('/otherpages/member/address/address', params);
		},
		//获取默认地址
		getAddressDetail() {
			this.$api.sendRequest({
				url: '/api/memberaddress/info',
				data: {
					default: 1
				},
				success: res => {
					let data = res.data;
					if (data != null) {
						this.address = data
						console.log(this.address.name);
					}
				},
			});
		},
		getOrderPaymentData() {
			this.$api.sendRequest({
				url: '/turntable/api/Turntable/getPrizeDetail',
				data: {
					award_id: this.award_id,
					record_id: this.record_id
				},
				success: res => {
					if (res.code >= 0) {
						this.goodsData = res.data
						this.goodsData.pay_price = this.goodsData.purchase_price;
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					} else {
						this.$util.showToast({
							title: '未获取到奖品信息！',
						});
						setTimeout(() => {
							this.$util.redirectTo('/pages/index/index/index', {}, 'reLaunch');
						}, 1000);
					}
				},
				fail: res => {
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			})
		},
		// 订单创建
		orderCreate() {
			if (this.verify()) {
				if (this.isSub) return;
				this.isSub = true;

				var data = this.$util.deepClone(this.orderCreateData);
				data.delivery = JSON.stringify(data.delivery);
				data.member_address = JSON.stringify(data.member_address);

				this.$api.sendRequest({
					url: '/turntable/api/Turntable/create',
					data:{
						record_id:this.record_id,
						member_address:this.address.full_address + this.address.address,
						buyer_message:this.buyer_message,
						is_balance: this.goodsData.is_balance
					},
					success: res => {
						if (res.code >= 0) {
							if (res.data.payment ==  1) { //需要去支付
								this.$refs.choosePaymentPopup.getPayInfo(res.data.out_trade_no);
								this.isSub = false;
							} else {
								this.$util.redirectTo('/promotionpages/game/result/result', {id:this.goodsData.game_id},
									'redirectTo');
							}
						} else {
							this.isSub = false;
							if (res.data.error_code == 10 || res.data.error_code == 12) {
								uni.showModal({
									title: '订单未创建',
									content: res.message,
									confirmText: '去设置',
									success: res => {
										if (res.confirm) {
											this.selectAddress();
										}
									}
								})
							} else {
								this.$util.showToast({
									title: res.message
								});
							}
						}
					},
					fail: res => {
						uni.hideLoading();
						this.isSub = false;
					}
				})
			}
		},
		// 订单验证
		verify() {
			if (!this.address.full_address) {
				this.$util.showToast({
					title: '请先选择您的收货地址'
				});
				return false;
			}
			return true;
		},
		imgError() {
			let imageUrl = ''
			if (this.orderPaymentData.exchange_info.type == 1) {
				imageUrl = this.$util.img('upload/uniapp/point/gift.png');
			} else if (this.orderPaymentData.exchange_info.type == 2) {
				imageUrl = this.$util.img('upload/uniapp/point/coupon.png');
			} else if (this.orderPaymentData.exchange_info.type == 3) {
				imageUrl = this.$util.img('upload/uniapp/point/hongbao.png');
			} else {
				imageUrl = this.$util.getDefaultImage().default_goods_img;
			}
			this.orderPaymentData.exchange_info.image = imageUrl;
			this.$forceUpdate();
		},
	},
	onShow() {
		// 刷新多语言
		this.$langConfig.refresh();
		// 判断登录
		if (!uni.getStorageSync('token')) {
			this.$util.redirectTo('/otherpages/login/login/login');
		} else {
			this.getOrderPaymentData();
			this.getAddressDetail()
		}

		this.isIphoneX = this.$util.uniappIsIPhoneX();
	},
	filters: {
		// 金额格式化输出
		moneyFormat(money) {
			return parseFloat(money).toFixed(2);
		}
	}
}
